package com.bp.ice.controller;

import com.bp.ice.dao.iceUserDao11;
import com.bp.ice.po.userpo;
import com.bp.ice.service.iceUserService11;
import com.bp.ice.vo.resultSeting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/iceuser")
public class IceUserContoller11 {
    @Autowired
    private iceUserService11 iceUserService11;
    @Autowired
    private iceUserDao11 iceUserDao11;
    @RequestMapping("/checkUser")
    public resultSeting checkUser(String username, String password, HttpServletResponse response){
        Integer checkid = iceUserService11.check(username, password);
        if(checkid!=null){
            String message1="验证成功";
            response.addCookie(new Cookie("cookie",username));
            return  new resultSeting(message1);
        }else {
            String message2="验证失败";
            return new resultSeting(message2);
        }
    }
    @RequestMapping("/iceManager")
    public resultSeting iceManager(String username,String password){
        if("Manager".equals(username) && "123456".equals(password)){
            String back="进入后台管理页面";
            return  new resultSeting(back);
        }
        return null;
    }
    @RequestMapping("/addUser")
    public resultSeting addUser(String username,String password){
        Integer row = iceUserService11.addUser(username, password);
        if(row>0){
            return new resultSeting("注册成功");
        }else{
            return new resultSeting("注册失败");
        }
    }
    @RequestMapping("/findPassword")
    public resultSeting findPassword(String mobile,String yzm,String username){
        List<String> mexit = iceUserDao11.Mexit();
        boolean cc=false;
        for(String ss:mexit){
            if(mobile.equals(ss)){
                cc=true;
            }
        }
        if(cc){
            String msg="通过电话号码进行找回";
            String password = iceUserService11.findPassword(mobile, yzm, username);
            List<String> re=new ArrayList<>();
            re.add(msg);re.add(password);
            return new resultSeting(re);
        }else {
            String msg="通过识别码进行找回";
            String password=iceUserService11.findPassword(mobile, yzm, username);
            List<String> re=new ArrayList<>();
            if(password==null){
                msg="数据不存在";
            }
            re.add(msg);re.add(password);
            return new resultSeting(re);
        }
    }
    @RequestMapping("/UpdateUser")
    public String UpdateUser(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie:cookies){
            String coo="cookie";
            coo.equals(cookie.getName());
            return cookie.getValue();
        }
        return null;
    }
    @RequestMapping("/display")
    public  resultSeting display(String username){
        List<userpo> display = iceUserService11.display(username);
        return  new resultSeting(display);
    }
    @RequestMapping("/storeUser")
    public resultSeting storeUser(userpo po){
        Integer row = iceUserService11.storeUser(po);
        if (row>0){
            return new resultSeting("更新成功");
        }else {
            return new resultSeting("更新失败");
        }
    }
}
